//
//  WKWebView+Images.m
//  DookayProject
//
//  Created by jim on 2020/5/22.
//  Copyright © 2020 Dookay. All rights reserved.
//

#import "WKWebView+Images.h"
#import "WSLPictureBrowseView.h"
#import <objc/runtime.h>

@interface WKWebView ()

@property (nonatomic, strong) NSMutableArray  *imgUrlArray;

@end

@implementation WKWebView (Images)

static const char *key = "imgs";

- (WKNavigationActionPolicy)decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction {
    
    //预览图片
    NSURL * url = navigationAction.request.URL;
    if ([url.scheme isEqualToString:@"image-preview-index"]) {
        //图片点击回调
        NSInteger index = [[url.absoluteString substringFromIndex:[@"image-preview-index:" length]] integerValue];
        NSString * imgPath = self.imgUrlArray.count > index? self.imgUrlArray[index]:nil;
        NSLog(@"imgPath = %@",imgPath);

        WSLPictureBrowseView * browseView = [[WSLPictureBrowseView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        browseView.backgroundColor = [UIColor blackColor];
        browseView.viewController = [VCManager shareVCManager].getTopViewController;
        browseView.urlArray = [NSMutableArray arrayWithArray:self.imgUrlArray];
        browseView.index = index;
        [KWindow addSubview:browseView];

        return  WKNavigationActionPolicyCancel;
    } else {
        return  WKNavigationActionPolicyAllow;
    }
}

- (void)registerImageClickAction{
    
    //获取图片标签
    [self evaluateJavaScript:@"function getImages(){\
     var imgs = document.getElementsByTagName('img');\
     var imgScr = '';\
     for(var i=0;i<imgs.length;i++){\
     if (i == 0){ \
     imgScr = imgs[i].src; \
     } else {\
     imgScr = imgScr +'***'+ imgs[i].src;\
     } \
     };\
     return imgScr;\
     };" completionHandler:nil];//注入js方法
    
    __weak typeof(self)weakSelf = self;
    [self evaluateJavaScript:@"getImages()" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
        
        if (!error) {
            
            NSMutableArray * urlArray = result?[NSMutableArray arrayWithArray:[result componentsSeparatedByString:@"***"]]:nil;
            NSLog(@"urlArray = %@",urlArray);
            weakSelf.imgUrlArray = urlArray;
        } else {
            weakSelf.imgUrlArray = [NSMutableArray array];
        }
    }];
    
    //添加图片点击的回调
    [self evaluateJavaScript:@"function registerImageClickAction(){\
     var imgs = document.getElementsByTagName('img');\
     for(var i=0;i<imgs.length;i++){\
     imgs[i].customIndex = i;\
     imgs[i].onclick=function(){\
     window.location.href='image-preview-index:'+this.customIndex;\
     }\
     }\
     }" completionHandler:nil];
    [self evaluateJavaScript:@"registerImageClickAction();" completionHandler:nil];
}

- (void)setImgUrlArray:(NSMutableArray *)imgUrlArray{
    objc_setAssociatedObject(self, key, imgUrlArray, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSMutableArray *)imgUrlArray{
    return objc_getAssociatedObject(self, key);
}

@end
